package com.example.demo01.adapter


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.core.graphics.toColorInt
import com.example.demo01.R

class ViewPager2Adapter : RecyclerView.Adapter<ViewPager2Adapter.PageViewHolder>() {

    // 数据类
    //一共有四个属性 颜色 + 文本 +  图片 + 标题
    data class PageData(val color: String, val text: String, val img: Int, val detill: String)

    // 页面数据：颜色 + 文本
    private val pages = listOf(
        PageData("#FF5733", "Page 1", R.drawable.bj, "这是第一个页面"),
        PageData("#33FF57", "Page 2", R.drawable.bj, "这是第二个页面"),
        PageData("#3357FF", "Page 3", R.drawable.bj, "这是第三个页面")
    )

    inner class PageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.pageText)
        val iconView: ImageView = itemView.findViewById(R.id.pageImage)
        val detillView: TextView = itemView.findViewById(R.id.pageDetill)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_page, parent, false)
        return PageViewHolder(view)
    }


    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        val page = pages[position]
        with(holder) {
            textView.text = page.text
            itemView.setBackgroundColor(page.color.toColorInt())
            iconView.setImageResource(page.img)
            detillView.text = page.detill
        }
    }

    override fun getItemCount() = pages.size

}